package cn.ultrarisk.scadmin.manager.web.controller.solr;

import cn.ultrarisk.mybatis.extension.domain.Page;
import cn.ultrarisk.mybatis.extension.query.Query;
import cn.ultrarisk.scadmin.manager.biz.monitor.dao.SolrReplicaMonitorDAO;
import cn.ultrarisk.scadmin.manager.biz.solr.context.SolrState;
import cn.ultrarisk.scadmin.manager.biz.solr.dao.SolrCollectionDAO;
import cn.ultrarisk.scadmin.manager.biz.solr.dao.SolrNodeDAO;
import cn.ultrarisk.scadmin.manager.biz.solr.dao.SolrReplicaDAO;
import cn.ultrarisk.scadmin.manager.biz.solr.dao.SolrShardDAO;
import cn.ultrarisk.scadmin.manager.biz.solr.entity.SolrCollection;
import cn.ultrarisk.scadmin.manager.biz.solr.entity.SolrNode;
import cn.ultrarisk.scadmin.manager.biz.solr.entity.SolrReplica;
import cn.ultrarisk.scadmin.manager.biz.solr.entity.SolrShard;
import cn.ultrarisk.scadmin.manager.biz.solr.service.SolrReplicaService;
import cn.ultrarisk.scadmin.manager.web.controller.BaseController;
import cn.ultrarisk.scadmin.manager.web.dto.solr.SolrReplicaDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * NodeController
 *
 * @author sevendlong(lin23871@163.com)
 * @date 2014/4/17
 * @since V1.0
 */
@Controller
@RequestMapping(value = "/solr/replica")
public class ReplicaController extends BaseController {
    @Autowired
    private SolrReplicaDAO solrReplicaDAO;
    @Autowired
    private SolrReplicaService solrReplicaService;
    @Autowired
    private SolrNodeDAO solrNodeDAO;
    @Autowired
    private SolrCollectionDAO solrCollectionDAO;
    @Autowired
    private SolrReplicaMonitorDAO solrReplicaMonitorDAO;
    @Autowired
    private SolrShardDAO solrShardDAO;

    @ResponseBody
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseEntity<Map<String, Object>> list(String host, Integer collectionId, SolrState state, int pageNo, int pageSize) {
        Map<String, Object> map = new HashMap<String, Object>();

        Page<SolrReplica> paged = solrReplicaDAO.findPage(host, collectionId, state, pageNo, pageSize);
        map.put("paged", paged);
        map.put("collections", solrCollectionDAO.findList(null, true));
        map.put("hosts", solrNodeDAO.findColumn(new Query(), SolrNode.HOST, true, true));
        map.put("states", SolrState.values());

        Map<Integer, Integer> collectionIds = new HashMap<Integer, Integer>();
        Map<Integer, Integer> nodeIds = new HashMap<Integer, Integer>();
        Map<Integer, Integer> shardIds = new HashMap<Integer, Integer>();
        List<Integer> replicaIds = new ArrayList<Integer>();
        if (paged.getList().size() > 0) {
            for (SolrReplica replica : paged.getList()) {
                collectionIds.put(replica.getCollectionId(), 0);
                nodeIds.put(replica.getNodeId(), 0);
                shardIds.put(replica.getShardId(), 0);
                replicaIds.add(replica.getId());
            }

            if (collectionIds.size() > 0) {
                map.put("collectionMap", solrCollectionDAO.findEntityList(Query.inQuery(SolrCollection.ID, collectionIds.keySet()), SolrCollection.ID, SolrCollection.NAME));
            }
            if (nodeIds.size() > 0) {
                map.put("nodeMap", solrNodeDAO.findEntityList(Query.inQuery(SolrNode.ID, nodeIds.keySet()), SolrCollection.ID, SolrNode.NAME));
            }
            if (shardIds.size() > 0) {
                map.put("shardMap", solrShardDAO.findEntityList(Query.inQuery(SolrShard.ID, shardIds.keySet()), SolrShard.ID, SolrShard.NAME));
            }
            map.put("monitorMap", solrReplicaMonitorDAO.getNewestReplicaDocs(replicaIds));
        }
        return getOkResponseEntity(map);
    }

    @ResponseBody
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseEntity<String> create(@RequestBody SolrReplicaDTO dto) {
        solrReplicaService.create(dto.getCoreName(), dto.getCollectionId(), dto.getShardName(), dto.getNodeId(), true);
        return getOkResponseEntity();
    }

    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<String> delete(@PathVariable("id") Integer id) throws Exception {
        solrReplicaService.delete(id, true);
        return getOkResponseEntity();
    }

    @ResponseBody
    @RequestMapping(value = "/collection/{collectionId}", method = RequestMethod.GET)
    public ResponseEntity<List<Map<String, Object>>> queryByCollectionId(@PathVariable("collectionId") Integer collectionId) {
        List<SolrReplica> replicas = solrReplicaDAO.findAvailable(null, collectionId, false);
        Map<Integer, SolrShard> shardMap = solrShardDAO.findMap(Query.query(SolrShard.COLLECTION_ID, collectionId), SolrShard.ID, SolrShard.NAME);
        List<Map<String, Object>> replicaMapList = new ArrayList<Map<String, Object>>();
        for (SolrReplica replica : replicas) {
            Map<String, Object> replicaMap = new HashMap<String, Object>();
            replicaMap.put("coreName", replica.getCoreName());
            replicaMap.put("nodeId", replica.getNodeId());
            replicaMap.put("shard", shardMap.get(replica.getShardId()).getName());
            replicaMapList.add(replicaMap);
        }
        return getOkResponseEntity(replicaMapList);
    }
}
